package com.lanou.connotationjoke.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.entity.LoginUserEty;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 16/1/27.
 */
public class UserDataActivity extends BaseActivity implements View.OnClickListener {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_data);
        findViewById(R.id.tv_log_out).setOnClickListener(this);

        sharedPreferences = getSharedPreferences("userData",MODE_PRIVATE);
        editor = sharedPreferences.edit();

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_log_out:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("退出确认");
                builder.setMessage("确认退出当前账号吗?");
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Platform qq = ShareSDK.getPlatform(UserDataActivity.this, QQ.NAME);
                        if(qq.isValid()) {
                            qq.removeAccount();
                              editor.clear();
                              editor.commit();
                            //通知framgment页面通过发送一个无效的网址另头像显示加载错误时的图片也就是默认图片
                            EventBus.getDefault().post(new LoginUserEty());
                            Intent intent = new Intent(UserDataActivity.this,LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
                builder.setNegativeButton("取消",null);
                builder.show();
            break;
        }
    }

}
